package com.sj.interfaces;

import com.sj.domain.Account;
import com.sj.domain.AccountRepository;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by yangrd on 2018/3/8
 **/
@AllArgsConstructor
@RestController
public class AccountController {

    private AccountRepository repository;

    @GetMapping("/account")
    public ResponseEntity<Account> findByName(String name){
        Account account = repository.findOne(Example.of(Account.builder().name(name).build()));
        return ResponseEntity.ok(account);
    }

    @GetMapping("/accounts")
    public ResponseEntity<Page<Account>> findByName(String name, Pageable pageable){
        Page<Account> accountPage = repository.findAll(Example.of(Account.builder().name(name).build()),pageable);
        return ResponseEntity.ok(accountPage);
    }

}
